class Animal09 {
    @Override
    public String toString() {
        return "This is an Animal.";
    }
}

class Dog09 extends Animal09 {
    private String name;

    public Dog09(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Dog name=" + name;
    }

    public void watchHome() {
        System.out.println("Dog is watching home.");
    }
}

class Cat09 extends Animal09 {
    private String name;

    public Cat09(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Cat name=" + name;
    }

    public void catchMouse() {
        System.out.println("Cat is catching a mouse.");
    }
}

// 测试类
public class work09 {
    public static void main(String[] args) {
        // 多态：父类引用指向子类对象
        Animal09 animal1 = new Dog09("Dog");
        Animal09 animal2 = new Cat09("Cat");

        // 调用 toString()
        System.out.println(animal1);
        System.out.println(animal2);

        // 强制类型转换并调用子类特有方法
        if (animal1 instanceof Dog09) {
            Dog09 dog = (Dog09) animal1;
            dog.watchHome();
        }

        if (animal2 instanceof Cat09) {
            Cat09 cat = (Cat09) animal2;
            cat.catchMouse();
        }
    }
}

